//
//  ActivityViewController.m
//  UI14_HomeWork_豆瓣
//
//  Created by Bodi on 16/6/23.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ActivityViewController.h"
#import "ActivityModel.h"
#import "CustomTableViewCell.h"
#import "SecondViewController.h"
@interface ActivityViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain)NSMutableArray *modelArray;
@property (nonatomic, retain)UITableView *tableView;
@end

@implementation ActivityViewController
static NSString *const newsCellId = @"newsCell";
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self config];
    [self hanldData];
    [self createSubView];
    
}
- (void)config{
    
self.navigationItem.title = @"活动";
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:0.41f green:0.61f blue:0.68f alpha:1.00f];

}
- (void)hanldData{
    self.modelArray = [NSMutableArray array];
    NSString *path = [[NSBundle mainBundle]pathForResource:@"activitylist" ofType:@"txt"];
    NSData *data = [NSData dataWithContentsOfFile:path];
    NSError *error = nil;
    NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
    NSArray *resultArray = [dictionary objectForKey:@"events"];
    for (NSDictionary *dic in resultArray) {
        ActivityModel *activityModel = [[ActivityModel alloc]init];
        [activityModel setValuesForKeysWithDictionary:dic];
//        NSArray *ownerArray = [dic objectForKey:@"owner"];
        [self.modelArray addObject:activityModel];
        [activityModel release];
        NSLog(@"%@", self.modelArray);
        
        
    }
    
    
    
    


}
- (void)createSubView{
    UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    
    tableView.delegate = self;
    tableView.dataSource = self;
    
    
    
    [self.view addSubview:tableView];
    [tableView release];
    [tableView registerClass:[CustomTableViewCell class] forCellReuseIdentifier:newsCellId];



}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.modelArray.count;

}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:newsCellId];
    
    ActivityModel *model = self.modelArray[indexPath.row];
    
    cell.model = model;
    
    return cell;
 


}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 200;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    SecondViewController *sec = [[SecondViewController alloc]init];
    sec.actionModel = [self.modelArray objectAtIndex:indexPath.row];
    [self.navigationController pushViewController:sec animated:YES];


}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
